/*
https://leetcode-cn.com/problems/wildcard-matching/solution/tong-pei-fu-pi-pei-by-leetcode-solution/
 */
public class Solution44 {
    public boolean isMatch(String s, String p) {
        boolean[][] f=new boolean[s.length()+1][p.length()+1];
        f[0][0]=true;
        int c=0;
        while (c<p.length() && p.charAt(c)=='*'){
            f[0][c+1]=true;
            c++;
        }
        for (int i=1;i<=s.length();i++){
            for (int j=1;j<=p.length();j++){
                if (p.charAt(j-1)=='*'){
                    f[i][j]=f[i-1][j]|f[i][j-1];
                }else if (p.charAt(j-1)=='?'){
                    f[i][j]=f[i-1][j-1];
                }else{
                    f[i][j]=s.charAt(i-1)==p.charAt(j-1)&&f[i-1][j-1];
                }
            }
        }
        return f[s.length()][p.length()];
    }

    public static void main(String[] args) {
        System.out.println(new Solution44().isMatch("abcabczzzde","*abc???de*"));
    }
}
